package com.example.zzl.view.activity.UserActivity;

import android.content.Intent;
import android.databinding.DataBindingUtil;
import android.graphics.Color;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.example.zzl.R;
import com.example.zzl.app.MyApplication;
import com.example.zzl.databinding.ActivityLoginBinding;
import com.example.zzl.gen.UserDao;
import com.example.zzl.model.bean.User;
import com.example.zzl.utils.GreenDaoManager;

import java.util.ArrayList;
import java.util.HashMap;

import cn.smssdk.EventHandler;
import cn.smssdk.SMSSDK;
import cn.smssdk.gui.RegisterPage;

public class LoginActivity extends AppCompatActivity implements View.OnClickListener{

    private ActivityLoginBinding binding;
    private EditText et_phone,et_code,et_user,et_pwd;
    private TextView tv_phone_login,tv_user_login,tv_send,tv_phone_divide,tv_user_divide,tv_regist,tv_froget,tv_login_1,tv_login_2;
    private RelativeLayout rl_1,rl_2;
    private UserDao userDao;
    private int count;
    private String nichName;
    private boolean login = false;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getSupportActionBar().hide();
        binding = DataBindingUtil.setContentView(this,R.layout.activity_login);
        init();
    }

    private void init() {
        userDao = GreenDaoManager.getInstance(this).getNewSession().getUserDao();
        binding.setMyClick(this);
        et_phone = binding.etPhone;//手机号
        et_code = binding.etYanzheng;//验证码
        tv_send = binding.tvYanzheng;//发送验证码
        et_user = binding.etUser;//账号
        et_pwd = binding.etPwd;//密码

        tv_phone_login = binding.tvPhone;//手机登录
        tv_user_login = binding.tvNumber;//账号登录
        tv_phone_divide = binding.tvPhoneDivide;
        tv_user_divide = binding.tvNumberDivide;
        tv_regist = binding.tvRegist;//立即注册
        tv_froget = binding.tvFroget;//忘记密码

        tv_login_1 = binding.tvLogin;//手机登录按钮
        tv_login_2 = binding.tvLogin1;//账号登录按钮
        rl_1 = binding.rlLogin;
        rl_2 = binding.rlLogin1;

        //et_user.addTextChangedListener(this);
        et_phone.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
                if(TextUtils.isEmpty(et_phone.getText())){
                    tv_send.setBackgroundResource(R.drawable.login_edit);
                    tv_send.setHintTextColor(Color.parseColor("#CDCDCD"));
                    tv_login_1.setBackgroundResource(R.drawable.login_edit_2);
                    login = false;
                }else {
                    tv_send.setBackgroundResource(R.drawable.login_edit_3);
                    tv_send.setHintTextColor(Color.RED);
                    if(!TextUtils.isEmpty(et_code.getText())){
                        tv_login_1.setBackgroundResource(R.drawable.login_edit_1);
                        login = true;
                    }
                }
            }

            @Override
            public void afterTextChanged(Editable editable) {

            }
        });

        et_code.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
                if(TextUtils.isEmpty(et_code.getText())){
                    tv_login_1.setBackgroundResource(R.drawable.login_edit_2);
                    login = false;
                }else{
                    if(!TextUtils.isEmpty(et_phone.getText())) {
                        tv_login_1.setBackgroundResource(R.drawable.login_edit_1);
                        login = true;
                    }else{
                        tv_login_1.setBackgroundResource(R.drawable.login_edit_2);
                        login = false;
                    }
                }
            }

            @Override
            public void afterTextChanged(Editable editable) {

            }
        });



    }

    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.tv_phone://手机登录
                et_user.setText("");
                et_pwd.setText("");
                tv_phone_login.setTextColor(Color.RED);
                rl_1.setVisibility(View.VISIBLE);
                tv_froget.setVisibility(View.INVISIBLE);
                tv_phone_divide.setVisibility(View.VISIBLE);
                tv_user_divide.setVisibility(View.INVISIBLE);
                tv_user_login.setTextColor(Color.GRAY);
                rl_2.setVisibility(View.INVISIBLE);
                break;
            case R.id.tv_number://账号登录
                et_phone.setText("");
                et_code.setText("");
                tv_user_login.setTextColor(Color.RED);
                tv_froget.setVisibility(View.VISIBLE);
                rl_2.setVisibility(View.VISIBLE);
                tv_user_divide.setVisibility(View.VISIBLE);
                tv_phone_divide.setVisibility(View.INVISIBLE);
                tv_phone_login.setTextColor(Color.GRAY);
                rl_1.setVisibility(View.INVISIBLE);
                break;
            case R.id.tv_yanzheng://发送验证码
                        RegisterPage registerPage = new RegisterPage();
                        registerPage.setRegisterCallback(new EventHandler() {
                            public void afterEvent(int event, int result, Object data) {
// 解析注册结果
                                if (result == SMSSDK.RESULT_COMPLETE) {
                                    @SuppressWarnings("unchecked")
                                    HashMap<String,Object> phoneMap = (HashMap<String, Object>) data;
                                    String country = (String) phoneMap.get("country");
                                    String phone = (String) phoneMap.get("phone");
                                    et_code.setText(phone);
// 提交用户信息（此方法可以不调用）
                                    //   registerUser(country, phone);
                                }
                            }
                        });
                        registerPage.show(this);
                break;
            case R.id.tv_regist://立即注册
                startActivityForResult(new Intent(LoginActivity.this,RegistActivity.class),345);
                break;
            case R.id.tv_froget://忘记密码
                Toast.makeText(this, "忘记密码", Toast.LENGTH_SHORT).show();
                break;
            case R.id.iv_qq://qq登录
                Toast.makeText(this, "qq登录", Toast.LENGTH_SHORT).show();
                break;
            case R.id.iv_weibo://微博登录
                Toast.makeText(this, "微博登录", Toast.LENGTH_SHORT).show();
                break;
            case R.id.iv_login_close://关闭
                setResult(666,getIntent());
                finish();
                break;

            case R.id.tv_login://手机登录
                if(login) {
                    ArrayList<User> list = (ArrayList<User>) userDao.loadAll();
                    if (!TextUtils.isEmpty(et_phone.getText())) {
                        for (int i = 0; i < userDao.loadAll().size(); i++) {
                            if (et_phone.getText().toString().equals(list.get(i).getPhoneNum())) {
                                count += 1;
                                nichName = list.get(i).getNickName();
                                //Log.e("data",list.get(i).getPhoneNum()+"===");
                            }
                        }
                        //Log.e("data",count+"====");
                        if (count > 0) {
                            Toast.makeText(this, "登录成功", Toast.LENGTH_SHORT).show();
                            MyApplication.nichName = nichName;
                            //Log.e("data",nichName);
                            count = 0;
                            setResult(900,getIntent());
                            finish();
                        } else {
                            Toast.makeText(this, "登录失败", Toast.LENGTH_SHORT).show();
                            count = 0;
                        }
                    }
//                    et_code.setText("");
//                    et_phone.setText("");
                    login=false;

                }
                break;
            case R.id.tv_login_1://账号登录
                ArrayList<User> list1 = (ArrayList<User>) userDao.loadAll();
                if(!TextUtils.isEmpty(et_user.getText()) && !TextUtils.isEmpty(et_pwd.getText())){
                    for (int i = 0; i < userDao.loadAll().size(); i++) {
                        if(et_user.getText().toString().equals(list1.get(i).getPhoneNum()) || et_user.getText().toString().equals(list1.get(i).getNickName()) || et_user.getText().toString().equals(list1.get(i).getEmail()) ){
                            count+=1;
                            nichName = list1.get(i).getNickName();
                            //Log.e("data",list1.get(i).getPhoneNum()+"===");
                        }
                    }
                    //Log.e("data",count+"====");
                    if(count>0){
                        Toast.makeText(this, "登录成功", Toast.LENGTH_SHORT).show();
                        MyApplication.nichName = nichName;
                        //Log.e("data",nichName);
                        count = 0;
                        setResult(900,getIntent());
                        finish();
                    }else{
                        Toast.makeText(this, "登录失败", Toast.LENGTH_SHORT).show();
                        count = 0;
                    }
                }
                break;
        }
    }
}
